<template>
  <MrCustom bg-color="bg-gradual-blue " is-back>
    <template #backText>
      返回
    </template>
  </MrCustom>
  <view class="bg-white margin-xs radius" v-if="nurseInfo" style="min-height: 90vh;">
    <view class="bg-white radius">
      <view class="flex padding-sm justify-between">
        <view class="padding-xs text-xxl text-black">
          <view>{{ nurseInfo.name }}
            <text class="text-gray text-df"> / 主治</text>
          </view>
          <view class="text-gray text-df padding-top-sm">{{ nurseInfo.department }}</view>
        </view>
        <view class="">
          <view class="cu-avatar xl round"
                style="background-image:url(https://image.meiye.art/Fha6tqRTIwHtlLW3xuZBJj8ZXSX3?imageMogr2/thumbnail/450x/interlace/1);">
          </view>
        </view>
      </view>
    </view>
    <view class="cu-bar margin-top-sm">
      <view class="action">
        <text class="text-xl">我的评价</text>
      </view>
    </view>
    <view v-if="commentInfo">
      <view class="margin-lr-sm padding-left padding-top padding-bottom-xs">
        <text>
          {{ commentInfo.content }}
        </text>
      </view>
      <view class="flex justify-end text-gray text-sm margin-right">
        {{ commentInfo.createTime }}
      </view>
    </view>
    <view v-else class="padding">
      <uni-easyinput type="textarea" v-model="content" placeholder="请输入内容"></uni-easyinput>
      <view class="flex flex-direction margin-top">
        <button class="cu-btn bg-blue lg" @click="handleSubmit">提交</button>
      </view>
    </view>
  </view>

</template>

<script setup lang="ts">

import {onLoad, onUnload} from "@dcloudio/uni-app";
import {ref} from "vue";
import api from "@/api";
import {useUserStore} from "@/store";
import MrCustom from "@/components/common/MrCustom.vue";

const nurseInfo = ref()
const appointmentInfo = ref()
const userStore = useUserStore()
const content = ref()
const commentInfo = ref()


onLoad((option) => {
  appointmentInfo.value = JSON.parse(decodeURIComponent(option.appointmentInfo))
  getNurseInfo(appointmentInfo.value.nurseUsername)
  getCommentInfo(appointmentInfo.value.id, 1, appointmentInfo.value.patientUsername, appointmentInfo.value.nurseUsername)
})
onUnload(() => {
  let pages = getCurrentPages()
  let page = pages[pages.length - 2]
  if(page.route==='pages/patient/appointment/records/list'){
    page.$vm.pageReload()
  }
})
const getNurseInfo = (username: any) => {
  api.nurse.infoByUsername({
    username: username,
  }).then(res => {
    if (res && res.data.code === 0) {
      nurseInfo.value = res.data.data
    }
  })
}
const getCommentInfo = (targetId: any, targetType: any, fromUsername: any, toUsername: any) => {
  api.comment.queryByTargetIdBothUsername({
    targetId: targetId,
    targetType: targetType,
    fromUsername: fromUsername,
    toUsername: toUsername
  }).then(res => {
    if (res && res.data.code === 0) {
      commentInfo.value = res.data.data[0]
    }
  })
}

/**
 * 预约就诊提交评价 获取评价 显示
 * 医护评价
 */
const handleSubmit = () => {
  api.comment.createCommentRoot({
    targetId: appointmentInfo.value.id,
    targetType: 1,
    fromUsername: userStore.userInfo.username,
    fromAvatar: userStore.userInfo.avatar,
    fromName: '  ',
    toUsername: nurseInfo.value.username,
    content: content.value,
  }).then(res => {
    if (res && res.data.code === 0) {
      commentInfo.value = res.data.data
      uni.showToast({
        title: res.data.msg,
        icon: 'success',
      })
    }
  })
}
</script>

<style scoped>

</style>